package com.lanou.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;

import com.lanou.myapplication.database.UserDBHelper;
import com.lanou.myapplication.database.greendao.PersonEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class RegisterActivity extends Activity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private Button finishBtn;
    private EditText username_edit,password_edit,password_edit_same,specialname_input;
    private  RadioGroup sexlayout;
    private RadioButton radioButton;
    private UserDBHelper helper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }

    private void init() {
        helper = UserDBHelper.getInstance();
        finishBtn=(Button)findViewById(R.id.finishBtn);
        finishBtn.setOnClickListener(this);
        username_edit=(EditText)findViewById(R.id.username_edit);
        password_edit=(EditText)findViewById(R.id.password_edit);
        password_edit_same=(EditText)findViewById(R.id.password_edit_same);
        specialname_input=(EditText)findViewById(R.id.specialname_input);
        sexlayout=(RadioGroup)findViewById(R.id.sexlayout);
        sexlayout.setOnCheckedChangeListener(this);
       radioButton=(RadioButton)findViewById(sexlayout.getCheckedRadioButtonId());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.finishBtn:
                save();
         break;
            default:
                break;
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
          case  R.id.btn_man:
                Toast.makeText(this, "性别男",
                        Toast.LENGTH_SHORT).show();

              break;

            case  R.id.btn_womam:
                Toast.makeText(this, "性别女",
                        Toast.LENGTH_SHORT).show();
                break;
            default:
                break;

        }
    }
    private void save() {
        String name = username_edit.getText().toString();
        String password = password_edit.getText().toString();
        String specialname = specialname_input.getText().toString();
        String sex =radioButton.getText().toString();

        if(username_edit.length() >3) {

            List <PersonEntity> entity =helper.querByName(name);
            if (name.equals(entity.get(0).getName())){
                Toast.makeText(this,"用户名已存在",Toast.LENGTH_SHORT).show();
               }
            else{
                List<PersonEntity> entityInfor = new ArrayList<PersonEntity>();
                PersonEntity infor = new PersonEntity();

                infor.setName(name);
                infor.setPassword(password);
                infor.setSex(sex);
                infor.setSpecialname(specialname);
                entityInfor.add(infor);
                helper.addData(infor);

                gotoHome();
            }
        }else {
            Toast.makeText(this,"输入的用户名长度应该大于3",Toast.LENGTH_SHORT).show();
        }
    }

    private void gotoHome() {
        Intent intent =new Intent(this,LoginActivity.class);
        startActivity(intent);
        finish();
    }


}
